package com.tte.common.designpattern.State;

/**
 * State 接口和实现了 State 接口的实体状态类。Context 是一个带有某个状态的类。
 * StatePatternDemo，我们的演示类使用 Context 和状态对象来演示 Context 在状态改变时的行为变化
 */
public class StatePatternDemo {
    public static void main(String[] args) {
        ContextT context = new ContextT();

        StartStateT startState = new StartStateT();
        startState.doAction(context);

        System.out.println(context.getState().toString());

        StopStateT stopState = new StopStateT();
        stopState.doAction(context);

        System.out.println(context.getState().toString());
    }
}
/**
 * 带有状态的类
 */
class ContextT {
    private StateT state;

    public ContextT(){
        state = null;
    }

    public void setState(StateT state){
        this.state = state;
    }

    public StateT getState(){
        return state;
    }
}

class StopStateT implements StateT {

    public void doAction(ContextT context) {
        System.out.println("Player is in stop state");
        context.setState(this);
    }

    public String toString(){
        return "Stop State";
    }
}

class StartStateT implements StateT {

    public void doAction(ContextT context) {
        System.out.println("Player is in start state");
        context.setState(this);
    }

    public String toString(){
        return "Start State";
    }
}

/**
 * 状态接口
 */
interface StateT {
    public void doAction(ContextT context);
}